package org.example;

public class Main {
    public static void main(String[] args) {
        final long start = System.currentTimeMillis();
        int nodeNum = 500000; // 50万单链表节点
        LinkNode head = InitLinkNode(nodeNum);
        for (int i = 1; i <= 10; i++) {
            searchLinkData(head, nodeNum);
        }
        long costTime = System.currentTimeMillis() - start;
        System.out.printf("单次平均耗时：%d ms\n", costTime / 10);
    }

    // 根据指定节点数量，初始化链表
    static LinkNode InitLinkNode(int nodeNum) {
        LinkNode head = new LinkNode();
        LinkNode cursorLinkNode = head;

        for (int i = 1; i <= nodeNum; i++) {
            LinkNode tmpNode = new LinkNode();
            tmpNode.data = i;
            tmpNode.next = null;

            cursorLinkNode.next = tmpNode;
            cursorLinkNode = tmpNode;

        }

        return head;
    }

    // 查找指定数据的节点
    static void searchLinkData(LinkNode head, int destData) {
        LinkNode cursorLinkNode = head.next;
        int i = 0;
        while (cursorLinkNode != null) {
            i++;
            if (cursorLinkNode.data == destData) {
                System.out.printf("第 %d 次，寻找到了目标值：:%d\n", i, destData);
            }
            cursorLinkNode = cursorLinkNode.next;
        }
    }
}

class LinkNode {
    int data;
    LinkNode next;
}